/**
 * 回复评论
 * @author Qasim <15750783791@163.com>
 * @version 2016/11/11 1.0
 */
document.addEventListener('plusready' , ready , false);

var self = memberInfo = '';
function ready(){
	
	self = plus.webview.currentWebview();
	
	memberInfo = plus.storage.getItem('memberInfo');
	memberInfo = $.parseJSON(memberInfo);
	if(self.is_goods !== undefined)$('.good').find('i').css('color' , 'red');
	$('.msgid').attr('value' , self.aid)
	
	
	msgid = self.aid;
	$('.avatar').attr('src' , self.avatar);
	$('.nickname').text(self.nickname);
	$('.goods').text(self.goods);
	$('.words').text(self.title);
	$('.time').text(self.addtime);
	for(var i = 1 ; i <= self.level ; i++){
		$('.mui-lvs').append('&#xe627;') 
	} 
	//获取数据
	
	info();
	 
	
	//回复
	$('.reply').on('keydown' , function(e){
		var content = $(this).val();
		if(e.keyCode == 13){
			var msgid = $('.msgid').attr('value');
			if(msgid === self.aid && memberInfo.aid === self.uid){
				toast('你不能回复自己的消息');
				return; 
			}
			if(!content.trim()){ 
				toast('请输入内容');return;
			}
			reply(msgid , content);
		}
	}) 
	
	
	//点赞
	$('.good').on('click' , function(){
		$.ajax({
			type:"post",
			url:apiRoot + "/Home/Comments/updateGoods/",
			data:{
				id : self.aid,
				uid : memberInfo.aid
			},
			beforeSend:function(){
			plus.nativeUI.showWaiting('加载中...');
			},
			complete:function(){
				plus.nativeUI.closeWaiting();			
			}, 
			success:function(data){ 
				if(data == 1){ 
					toast('点赞成功');
					$('.good').find('i').css('color' , 'red');
					$('.goods').text(parseInt($('.goods').text())+1);
					if(self.is_second){
						mui.fire(plus.webview.getWebviewById(self.page) , 'updateComments' , {type:'good' , aid : self.aid})
					}else{
						mui.fire(plus.webview.getWebviewById(self.page) , 'addComment' , {})
					}
				}else{
					toast('您已赞过该消息');
				}
			},
			error:function(e){
				errorPrompt(e);
			}
		});
		
	})
}



function info(){
	$.ajax({
		url : apiRoot+ '/Home/Comments/replyInfo/',
		type : 'post',
		data:{ 
			id : self.aid
		},
		dataType:'json',
		beforeSend:function(){
			plus.nativeUI.showWaiting('加载中...');
		},
		complete:function(){
			plus.nativeUI.closeWaiting();			
		},
		success:function(data){ 
			if(data != ''){
				var html = '';
				$.each(data, function(k , v) {
					html += '<ul class="mui-table-view list" data-uid="'+v.uid+'" data-id="'+v.id+'"><li class="mui-table-view-cell mui-media">';
					html += '<a href="javascript:;"><img class="mui-media-object touxiang mui-pull-left" src="'+getImgUrl(v.avatar)+'">';
					html += '<div class="mui-media-body"><span class="mui-h5  mui-lan nickname">'+v.nickname+'</span>';
//					html += '<span class="gray mlr mui-h5">回复</span><span class="mui-h5  mui-lan">路飞</span>';
					html += '<p class="words">'+v.content+'</p><p class="time">'+v.addtime+'</p></div></a></li></ul>';
					if(v.second != ''){
						$.each(v.second, function(key , value) {
							html += '<ul class="mui-table-view" data-uid="'+value.uid+'" data-id="'+value.id+'"><li class="mui-table-view-cell mui-media">';
							html += '<a href="javascript:;"><img class="mui-media-object touxiang mui-pull-left" src="'+getImgUrl(value.avatar)+'">';
							html += '<div class="mui-media-body"><span class="mui-h5  mui-lan nickname">'+value.nickname+'</span>';
							html += '<span class="gray mlr mui-h5">回复</span><span class="mui-h5  mui-lan">'+v.nickname+'</span>';
							html += '<p class="words">'+value.content+'</p><p class="time">'+value.addtime+'</p></div></a></li></ul>';
						});
					}
				});
				$('.lists').html(html);
				$('.list').unbind();
				$('.list').on('click' , function(){
					var uid = $(this).attr('data-uid') , id = $(this).attr('data-id'),name = $(this).find('.nickname').text();
					uid = 1;
					if(memberInfo.aid === uid){
						toast('你不能回复自己的消息');
						return;
					} 
					$('.msgid').attr('value' , id);
					$('.reply').attr('placeholder' , '回复 '+name+':');
					$(this).siblings().removeClass('list2');
					$(this).addClass('list2')
					$('.reply').focus(); 
				});
				
			}else{
				toast('暂无数据');
			}
		},
		error : function(e){
			errorPrompt(e);
		}
	})
}



window.addEventListener('mousedown' , function(){  
	$('.reply').on('blur' , function(){
		$('.list').siblings().removeClass('list2');
		$('.msgid').attr('value' , self.aid);
		$(this).attr('placeholder' , '请输入您要回复的内容 ');
	})
})

/**
 * @param {Int} id
 * @param {String} content
 */
function reply(id , content){
	$.ajax({
		type:"post",
		url:apiRoot +'/Home/Comments/addComment/',
		data : {
			bookid : self.bookid,
			uid : memberInfo.aid,
			parent_id : id,
			content : content
		},
		beforeSend:function(){
			plus.nativeUI.showWaiting('提交中...');
		},
		complete:function(){
			plus.nativeUI.closeWaiting();
		},
		success : function(data){
			if(data > 0){
				$('.reply').val('');
				toast('发表成功');
				info();
				if(self.is_second){
					mui.fire(plus.webview.getWebviewById(self.page) , 'updateComments' , {aid : self.aid})
				}else{
					mui.fire(plus.webview.getWebviewById(self.page) , 'addComment' , {})
				}
			}
		},
		error : function(e){
			errorPrompt(e);
		}
	});
}



